﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock;

namespace sy2_11
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int[] num;
        int[] prime;
        private bool IsPrime(int number)
        {
            if (number <= 1) return false;
            if (number == 2) return true;
            if (number % 2 == 0) return false;
            for (int i = 3; i <= Math.Sqrt(number); i += 2)
            {
                if (number % i == 0)
                    return false;
            }
            return true;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            int n = int.Parse(textBox1.Text);
            int min = int.Parse(textBox2.Text);
            int max = int.Parse(textBox3.Text);

            num = new int[n];
            prime = new int[n];
            int primeIndex = 0;

            Random rnd = new Random();
            for (int i = 0; i < n; i++)
            {
                num[i] = rnd.Next(min, max + 1);
                if (IsPrime(num[i]))
                {
                    prime[primeIndex] = num[i];
                    primeIndex++;
                }
            }
            textBox4.Text = string.Join(" ", num);
            int[] validPrime = new int[primeIndex];
            Array.Copy(prime, validPrime, primeIndex);
            button2.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int primeCount = 0;
            foreach (int p in prime)
            {
                if (p != 0) primeCount++;
            }
            int[] validPrime = new int[primeCount];
            int idx = 0;
            foreach (int p in prime)
            {
                if (p != 0)
                {
                    validPrime[idx] = p;
                    idx++;
                }
            }
            textBox5.Text = string.Join(" ", validPrime);
        }
    }
 }

